package handles

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehicle-mgr/cache"
	"vehicle-mgr/models"
)

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	car_code := c.PostForm("car_code")
	car_type := c.PostForm("car_type")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	if car_name == "" || car_code == "" || car_type == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.CarData{

		CarName: car_name,
		CarCode: car_code,
		CarType: car_type,
		Cid:     cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ConAdd(c *gin.Context) {
	contract_code := c.PostForm("contract_code")
	contract_name := c.PostForm("contract_name")
	contract_price, _ := strconv.Atoi(c.PostForm("contract_price"))

	if contract_code == "" || contract_name == "" || contract_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.Contract{
		ContractCode:  contract_code,
		ContractName:  contract_name,
		ContractPrice: contract_price,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarConList(c *gin.Context) {

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	/*
		SELECT
		  contracts.id AS ConId,
		  car_data.car_name AS CarName,
		  car_data.car_code AS CarCode,
		  car_data.car_type AS CarType,
		  contracts.contract_code AS ContractCode,
		  contracts.contract_name AS ContractName,
		  contracts.contract_price AS ContractPrice
		FROM
		  contracts
		  LEFT JOIN car_data ON contracts.id = car_data.cid
	*/
	var carCon []models.CarCon
	redis_key := "conList_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("contracts").
			Select(
				"contracts.id AS ConId",
				"car_data.car_name AS CarName",
				"car_data.car_code AS CarCode",
				"car_data.car_type AS CarType",
				"contracts.contract_code AS ContractCode",
				"contracts.contract_name AS ContractName",
				"contracts.contract_price AS ContractPrice",
			).
			Joins("LEFT JOIN car_data ON contracts.id = car_data.cid").
			Offset(offset).Limit(size).Find(&carCon)
		jsonData, _ := json.Marshal(carCon)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carCon)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": carCon,
	})
	return

}

func CarConDetail(c *gin.Context) {
	id := c.Query("id")
	var carCon models.CarCon
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("contracts").
			Select(
				"contracts.id AS ConId",
				"car_data.car_name AS CarName",
				"car_data.car_code AS CarCode",
				"car_data.car_type AS CarType",
				"contracts.contract_code AS ContractCode",
				"contracts.contract_name AS ContractName",
				"contracts.contract_price AS ContractPrice",
			).
			Joins("LEFT JOIN car_data ON contracts.id = car_data.cid").
			Where("contracts.id=?", id).Find(&carCon)
		jsonData, _ := json.Marshal(carCon)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carCon)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": carCon,
	})
	return

}
